//
//  UICollectionView+PPKit.swift
//  PPKit
//
//  Created by xiaopin on 16/8/31.
//  Copyright © 2016年 pinguo. All rights reserved.
//  https://gitee.com/xiaopindev/PPKit
//  https://github.com/xiaopindev
//  https://xiaopin.cnblogs.com

import UIKit

public extension UICollectionView {
    
    /// 注册手写方式的cell类对象
    ///
    /// - Parameters:
    ///   - cellClass: <#cellClass description#>
    ///   - reuseID: <#reuseID description#>
    func registerCellClass(_ cellClass: AnyClass, reuseID:String) {
        self.register(cellClass, forCellWithReuseIdentifier: reuseID)
    }
    
    /// 注册Xib
    ///
    /// - Parameter cellName: 重用ID，值和Cell的文件名一致
    func registerCellNib(_ cellName:String) {
        let nib = UINib(nibName: cellName, bundle: nil)
        self.register(nib, forCellWithReuseIdentifier: cellName)
    }

    /// 空数据显示提示
    ///
    /// - Parameter rowCount: 显示时0，不显示1
    func noData(_ rowCount: Int? = 0) {
        if rowCount == 0 {
            let bgView = UIView()
            bgView.frame = self.frame
            
            let imgView = UIImageView()
            imgView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
            imgView.image = UIImage(named: "bg_nodata")
            imgView.contentMode = .scaleAspectFit
            imgView.center = bgView.center
            bgView.addSubview(imgView)
            
            backgroundView = bgView
        } else {
            backgroundView = nil
        }
    }
    
    /**
     空收藏显示提示
     
     :param: rowCount 显示时0，不显示1
     */
    func noFavorites(_ rowCount: Int) {
        if rowCount == 0 {
            let bgView = UIView()
            bgView.frame = self.frame
            
            let imgView = UIImageView()
            imgView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
            imgView.image = UIImage(named: "bg_nofavorites")
            imgView.contentMode = .scaleAspectFit
            imgView.center = bgView.center
            bgView.addSubview(imgView)
            
            backgroundView = bgView
        } else {
            backgroundView = nil
        }
    }
    
    func noResult(_ rowCount: Int) {
        if rowCount == 0 {
            let bgView = UIView()
            bgView.frame = self.frame
            
            let imgView = UIImageView()
            imgView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
            imgView.image = UIImage(named: "bg_noresult")
            imgView.contentMode = .scaleAspectFit
            imgView.center = bgView.center
            bgView.addSubview(imgView)
            
            backgroundView = bgView
        } else {
            backgroundView = nil
        }
    }
    
    /**
     网络断开
    
     :param: rowCount 显示时0，不显示1
     */
    func networkFail(_ rowCount: Int) {
        if rowCount == 0 {
            let bgView = UIView()
            bgView.frame = self.frame
            
            let imgView = UIImageView()
            imgView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
            imgView.image = UIImage(named: "bg_networkfail")
            imgView.contentMode = .scaleAspectFit
            imgView.center = bgView.center
            bgView.addSubview(imgView)
            
            let button = UIButton()
            button.frame = imgView.frame
            button.center = imgView.center
            button.addTarget(self, action: Selector(("reloadNewData")), for: .touchUpInside)
            bgView.addSubview(button)
            
            backgroundView = bgView
        } else {
            backgroundView = nil
        }
    }
}
